<?php

namespace App\Model\Member;

use App\Component\DeviceIdentity\DeviceUser;
use App\Component\User;
use App\Core\BaseModel;
use App\Core\Exception\DefaultException;
use App\Core\Output;
use App\Util\CommonHelper;
use App\Util\UserIdentityHelper;
use EasySwoole\Component\Di;
use EasySwoole\ORM\Exception\Exception;
use EasySwoole\Redis\Redis;
use EasySwoole\RedisPool\RedisPool;

/**
 * CREATE TABLE `ss_member` (
 * `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',
 * `salt` char(9) NOT NULL DEFAULT '' COMMENT '密码种子',
 * `password` varchar(128) NOT NULL DEFAULT '' COMMENT '密码md5（输入的值+salt）',
 * `username` varchar(255) NOT NULL DEFAULT '' COMMENT '用户名',
 * `name` varchar(128) NOT NULL DEFAULT '' COMMENT '姓名',
 * `phone` varchar(12) NOT NULL DEFAULT '' COMMENT '手机号',
 * `referrer_id` int(11) NOT NULL DEFAULT '0' COMMENT '推荐人',
 * `usdt` varchar(255) NOT NULL DEFAULT '' COMMENT 'usdt地址',
 * `binance` varchar(255) NOT NULL DEFAULT '' COMMENT '币安地址',
 * `trx` varchar(255) NOT NULL DEFAULT '' COMMENT '波场',
 * `alipay_qr` varchar(255) NOT NULL DEFAULT '' COMMENT '支付宝收款码',
 * `wepay_qr` varchar(255) NOT NULL DEFAULT '' COMMENT '微信收款码',
 * `pub_bank_name` varchar(255) NOT NULL DEFAULT '' COMMENT '对公收款银行',
 * `pub_bank_deposit` varchar(255) NOT NULL DEFAULT '' COMMENT '对公开户银行',
 * `pub_bank_account_name` varchar(255) NOT NULL DEFAULT '' COMMENT '对公开户姓名',
 * `pub_bank_account_no` varchar(255) NOT NULL DEFAULT '' COMMENT '对公银行帐号',
 * `pri_bank_name` varchar(255) NOT NULL DEFAULT '' COMMENT '对私收款银行',
 * `pri_bank_deposit` varchar(255) NOT NULL DEFAULT '' COMMENT '对私开户银行',
 * `pri_bank_account_name` varchar(255) NOT NULL DEFAULT '' COMMENT '对私开户姓名',
 * `pri_bank_account_no` varchar(255) NOT NULL DEFAULT '' COMMENT '对私银行帐号',
 * `score` decimal(10,2) NOT NULL COMMENT '积分代币',
 * `profit_score` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '收益账户',
 * `profit` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '单利润',
 * `locked_day` tinyint(3) NOT NULL DEFAULT '30' COMMENT '本金账户不可提现天数',
 * `state` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '账号状态[0:停用 1:启用]',
 * `remark` varchar(255) NOT NULL DEFAULT '' COMMENT '备注/员工介绍',
 * `created_at` datetime NOT NULL DEFAULT '1970-01-01 00:00:00' COMMENT '创建时间',
 * `created_by` varchar(32) NOT NULL DEFAULT '' COMMENT '创建人员账号',
 * `updated_at` datetime NOT NULL DEFAULT '1970-01-01 00:00:00' ON UPDATE CURRENT_TIMESTAMP COMMENT '最后更新时间',
 * `updated_by` varchar(32) NOT NULL DEFAULT '' COMMENT '最后更新人员账号',
 * `is_delete` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否删除[0:未删除，1：已删除]',
 * `deleted_at` datetime NOT NULL DEFAULT '1970-01-01 00:00:00' COMMENT '删除时间',
 * PRIMARY KEY (`id`) USING BTREE
 * ) ENGINE=InnoDB AUTO_INCREMENT=4611714 DEFAULT CHARSET=utf8mb4 COMMENT='用户表';
 */
/**
 * Class MemberModel
 * @property int $id
 * @property string $username
 * @property string $salt
 * @property string $password
 * @property string $email
 * @property string $name
 * @property string $phone
 * @property string $referrer_id
 * @property string $usdt
 * @property string $binance
 * @property string $trx
 * @property string $alipay_qr
 * @property string $wepay_qr
 * @property string $pub_bank_name
 * @property string $pub_bank_deposit
 * @property string $pub_bank_account_name
 * @property string $pub_bank_account_no
 * @property string $pri_bank_name
 * @property string $pri_bank_deposit
 * @property string $pri_bank_account_name
 * @property string $pri_bank_account_no
 * @property string $score
 * @property string $profit_score
 * @property string $profit
 * @property string $locked_day
 * @property string $state
 * @property string $remark
 * @property string $created_at
 * @property string $created_by
 * @property string $updated_at
 */
class MemberModel extends BaseModel
{

    protected $tableName = 'ss_member';
//    protected static $logTableName = 'ss_member_log';

    protected $casts = [
        'id'                    => 'integer',
        'role_id'               => 'integer',
        'username'              => 'string',
        'salt'                  => 'string',
        'password'              => 'string',
        'name'                  => 'string',
        'phone'                 => 'string',
        'email'                 => 'string',
        'referrer_id'           => 'integer',
        'usdt'                  => 'string',
        'binance'               => 'string',
        'trx'                   => 'string',
        'alipay_qr'             => 'string',
        'wepay_qr'              => 'string',
        'pub_bank_name'         => 'string',
        'pub_bank_deposit'      => 'string',
        'pub_bank_account_name' => 'string',
        'pub_bank_account_no'   => 'string',
        'pri_bank_name'         => 'string',
        'pri_bank_deposit'      => 'string',
        'pri_bank_account_name' => 'string',
        'pri_bank_account_no'   => 'string',
        'score'                 => 'float',
        'profit_score'          => 'float',
        'profit'                => 'float',
        'locked_day'            => 'integer',
        'state'                 => 'integer',
        'remark'                => 'string',
        'created_at'            => 'string',
        'type'                  => 'integer',
    ];

    //本金账户不可提现天数
    const LOCKED_DAY = 30;
    const ADMIN_ROLE_ID = 1;
    const MAX_SP_RECURSION_DEPTH = 1; //递归层数
    const STATISTICS_INTERVAL = 3600; //统计用户数据间隔

    public static function getStateDict($key = null)
    {
        return self::getMap([
            self::DISABLED => '停用',
            self::ENABLED  => '正常',
        ], $key);
    }

    /**
     * 创建会员信息 createUser
     * @param string $username
     * @param string $password
     * @param string $salt
     * @param int $referrer_id
     * @return BaseModel|\EasySwoole\ORM\AbstractModel
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws \Throwable
     */
    public function createUser(string $username, string $password, string $salt, int $referrer_id = 0)
    {
        $this->connection('write');
        $data = [
            'username'        => $username,
            'salt'            => $salt,
            'password'        => $password,
            'referrer_id'     => $referrer_id,
            'last_profit_time'     => date('Y-m-d 00:00:15'),
            'last_login_time' => CommonHelper::currentTime()
        ];
        return self::insertData($data);
    }

    /**
     * 重置密码 resetPassword
     * @param string $user_id
     * @param string $password
     * @param string $salt
     * @param int $referrer_id
     * @param int $reset_password
     * @return BaseModel|\EasySwoole\ORM\AbstractModel
     * @throws \EasySwoole\Mysqli\Exception\Exception
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws \Throwable
     */
    public function resetPassword(string $user_id, string $password, string $salt, int $reset_password = 0)
    {
        $this->connection('write');
        $data = [
            'salt'           => $salt,
            'password'       => $password,
            'reset_password' => $reset_password,
        ];
        return self::updateData($data, ['id' => $user_id]);
    }

    /**
     * @param int $type 类型
     * @return array
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws \Throwable
     */
    public function userList(int $type = 0, bool $is_parent = false)
    {
        $where = [
//            'state'      => self::STATE_SUCCESS,
        ];
        return self::makeQueryModel($where)->field(['id', 'username'])->all()->toArray();
    }

    protected static function onAfterUpdate($model, $res)
    {
        self::delCache('memberCache:');
    }

    protected static function onAfterInsert($model, $res)
    {
        self::delCache('memberCache:');
    }
}
